//
//  ViewController.m
//  Exam11-2
//
//  Created by 孙晓晔 on 2017/4/16.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSXMLParserDelegate>
@property(nonatomic, strong) NSString *addr; // 临时保存meeting属性数据
@property(nonatomic, strong) NSMutableString *element;

@property(nonatomic, strong) NSMutableDictionary *memberDict; // 字典用于临时保存每个 member节点数据
@property(nonatomic, strong) NSMutableArray *memberArr; // 数组用于临时保存member节点数据
@property(nonatomic, strong) NSMutableDictionary *meetingDict; // 字典用于保存xml解析数据

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.meetingDict = [NSMutableDictionary dictionary];
    
    self.element = [[NSMutableString alloc]init];
    
    NSString *strPath = [[NSBundle mainBundle] pathForResource:@"my" ofType:@"xml"];
    
    NSURL *url = [NSURL fileURLWithPath:strPath];
    
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    
    parser.delegate = self;
    
    [parser parse];
}

// 输出解析的字典
- (IBAction)button1:(id)sender {
    NSLog(@"%@", self.meetingDict);
}

#pragma mark NSXMLParserDelegate
// 元素遍历开始
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    
    if ([elementName isEqualToString:@"meeting"]) {
        
        self.memberArr = [NSMutableArray array];
        
        if ([attributeDict objectForKey:@"addr"]) {
            self.addr = [attributeDict objectForKey:@"addr"];
            NSLog(@"addr = %@", self.addr);
        }
    }
    
    if ([elementName isEqualToString:@"member"]) {
        NSLog(@"member");
        
        self.memberDict = [NSMutableDictionary dictionary];
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    [self.element setString:@""];
    
    [self.element appendString:string];
}

// 元素遍历结束
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
    NSString *str = [[NSString alloc] initWithString:self.element];
    
    if ([elementName isEqualToString:@"creator"]) {
        NSLog(@"creator = %@", str);
        [self.memberArr addObject:str];
    }
    
    if ([elementName isEqualToString:@"name"]) {
        NSLog(@"name = %@", str);
        
        [self.memberDict setObject:str forKey:@"name"];
    }
    
    if ([elementName isEqualToString:@"age"]) {
        NSLog(@"age = %@", str);
        [self.memberDict setObject:str forKey:@"age"];
    }
    
    if ([elementName isEqualToString:@"member"]) {
        [self.memberArr addObject:self.memberDict];
    }
    
    if ([elementName isEqualToString:@"meeting"]) {
        [self.meetingDict setObject:self.memberArr forKey:self.addr];
        
    }
}

// 解析出错
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"%@", [parseError description]);
}

@end
